﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Bll;
using System.Data;
using System.Text;
using System.Net;
using System.IO;

namespace Accounting.admin.Professional
{
    public partial class ProfessionalAdd : System.Web.UI.Page
    {
        ModelPublic m = new ModelPublic();
        BllProfessional b = new BllProfessional();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Session["Name"] != null && Session["Type"] != null)
                {
                    txtAuthor.Text = Session["Name"].ToString();
                    txtDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");

                    dataBind();
                    UserType.Text = Session["Type"].ToString();

                    if (UserType.Text == "0")
                    {
                        Recommend.Visible = true;
                    }
                    else
                    {
                        Recommend.Visible = false;
                    }
                }
                else
                {
                    btnRest.Enabled = false;
                    ASubmit.Disabled = false;
                }
            }
        }

        private void dataBind()
        {
            m.NavId = "3";
            DataTable dt = b.SubNav_Inquire_All(m);
            foreach (DataRow my in dt.Rows)
            {
                string name = "-" + my["Name"].ToString() + "-";
                string id = my["Id"].ToString();
                NavSubList.Items.Add(new ListItem(name, id));
            }

            DataTable nav = b.Nav_Inquire_All();

            foreach(DataRow my in nav.Rows)
            {
                if(my["Id"].ToString()=="3")
                {
                    H2_Title.InnerText = "添加：" + my["Name"].ToString();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            m.Title = txtTitle.Text.Trim();
            m.Author = txtAuthor.Text.Trim();
            m.DateTime = txtDateTime.Text.Trim();
            m.Content = Content.Text;
            m.NavId = "3";
            m.NavSubId = NavSubList.SelectedValue;
            if (txtNumber.Text == "")
                m.Number = "0";
            else
                m.Number = txtNumber.Text.Trim();

            if (UserType.Text == "0")
            {
                m.Remarks = "NULL";
                if (Recommend.Checked)
                    m.Recommend = "True";
                else
                    m.Recommend = "False";
            }
            else
            {
                m.Remarks = "'stu'";
                m.Recommend = "false";
            }


            string message = b.Professional_Add(m);
            if (message == "1")
            {
                Response.Write("<script>alert('添加成功！')</script>");
                //发送到公共信息
                m.Title = txtTitle.Text.Trim();
                m.NavId = "3";
                m.NavSubId = NavSubList.SelectedValue;

                string id = b.Professional_Id(m);
                AddNews(id);


                if (Recommend.Checked)
                    Response.Redirect("./ProfessionalAdmin.aspx");
                else
                    Response.Redirect("./ProfessionalUnpublished.aspx");
            }
            else
            {
                Response.Write("<script>alert('" + message + "')</script>");
            }
        }

        protected void btnRest_Click(object sender, EventArgs e)
        {
            ClearContext();
        }

        private void ClearContext()
        {
            txtAuthor.Text = "";
            txtDateTime.Text = "";
            txtNumber.Text = "";
            txtTitle.Text = "";
            Content.Text = "";
        }

        private void AddNews(string id)
        {
            string stid = Request["stid"];//信息公开网站相应的类别
            string stracc = "ckuivf";
            string strpwd = "fcsaezxcbn3d";


            string istb = Request.Params["istb"];//
            if (!string.IsNullOrEmpty(istb))
            {
                Adddate(id, stid, txtTitle.Text.Trim(), null, txtDateTime.Text.Trim(), Content.Text, stracc, strpwd, "http://xxgk.wzvtc.cn/data/adddata");
            }
        }

        public string Adddate(string id, string stid, string title, string ctitle, string postdate, string Article, string unames, string upass, string url)
        {


            string requestBody = string.Format("iid={0}&tid={1}&title={2}&TitleColor={3}&postdate={4}&Article={5}&unames={6}&upass={7}"

               , HttpUtility.UrlEncode(id, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(stid, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(title, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(ctitle, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(postdate, Encoding.GetEncoding("utf-8"))
               , HttpUtility.UrlEncode(Article, Encoding.GetEncoding("utf-8"))
                , HttpUtility.UrlEncode(unames, Encoding.GetEncoding("utf-8"))
                  , HttpUtility.UrlEncode(upass, Encoding.GetEncoding("utf-8"))
               , string.Empty
                );
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.KeepAlive = false;
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] aryBuf = Encoding.GetEncoding("utf-8").GetBytes(requestBody);
            request.ContentLength = aryBuf.Length;
            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(aryBuf, 0, aryBuf.Length);
                writer.Close();
                writer.Dispose();
            }
            string ret = string.Empty;
            using (WebResponse response = request.GetResponse())
            {
                StreamReader reader = new StreamReader(response.GetResponseStream()
                    , Encoding.GetEncoding("utf-8")
                    );
                ret = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();
            }

            return ret;
        }
    }
}